package tuan.score;

import org.w3c.dom.Node;

public class LengthScore extends NodeScore {
	int length;
	String op;
	double score;

	public LengthScore(int length, String op, double score) throws Exception {
		if (op == null) {
			throw new Exception("null arguments");
		}
		this.length = length;
		this.op = op;
		this.score = score;
	}

	protected int getTextLen(Node node) {
		return node.getTextContent().trim().length();
	}

	@Override
	protected double getScore(Node node) {
		double defaultScore = 0.0;
		int textLen = getTextLen(node);

		if (op.equals(">")) {
			return textLen > length ? score : defaultScore;
		}
		if (op.equals(">=")) {
			return textLen >= length ? score : defaultScore;
		}
		if (op.equals("<")) {
			return textLen < length ? score : defaultScore;
		}
		if (op.equals("<=")) {
			return textLen <= length ? score : defaultScore;
		}
		if (op.equals("!=")) {
			return textLen != length ? score : defaultScore;
		}
		return defaultScore;
	}

}
